package com.ibeeking.found.content.b.rest.service.impl;

import com.ibeeking.found.common.config.feign.GlobalClient;
import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.entity.DictDTO;
import com.ibeeking.found.common.enums.DictTypeEnum;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.common.enums.SexEnum;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.content.b.rest.service.IBroadcastAnchorService;
import com.ibeeking.found.content.service.common.dos.BroadcastAnchorDO;
import com.ibeeking.found.content.service.common.param.BroadcastAnchorParam;
import com.ibeeking.found.content.service.common.query.BroadcastAnchorPageQuery;
import com.ibeeking.found.content.service.common.query.BroadcastAnchorQuery;
import com.ibeeking.found.content.service.common.vo.BroadcastAnchorVO;
import com.ibeeking.found.content.service.mapper.BroadcastAnchorMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.ParamCheckUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.List;

import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @ClassName BroadcastAnchorServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2022-02-18 14:11
 **/
@Service
public class BroadcastAnchorServiceImpl  extends ServiceImpl<BroadcastAnchorMapper, BroadcastAnchorDO> implements IBroadcastAnchorService {

    @Resource
    private BroadcastAnchorMapper broadcastAnchorMapper;
    @Resource
    private GlobalClient globalClient;

    @Override
    public List<BroadcastAnchorVO> list(BroadcastAnchorQuery broadcastAnchorQuery) {
        LambdaQueryWrapper<BroadcastAnchorDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(broadcastAnchorQuery.getAnchorName()), BroadcastAnchorDO::getAnchorName, broadcastAnchorQuery.getAnchorName());
        List<BroadcastAnchorDO> broadcastAnchorDOs = broadcastAnchorMapper.selectList(queryWrapper);
        List<BroadcastAnchorVO> broadcastAnchorVOS = JsonUtils.jsonToList(broadcastAnchorDOs, BroadcastAnchorVO.class);
        wrapperVO(broadcastAnchorVOS);
        return broadcastAnchorVOS;
    }

    @Override
    public Page<BroadcastAnchorVO> pageList(BroadcastAnchorPageQuery broadcastAnchorPageQuery) {
        Page<BroadcastAnchorDO> pages = broadcastAnchorMapper.selectPage(new Page<BroadcastAnchorDO>(), new LambdaQueryWrapper<BroadcastAnchorDO>()
                .eq(broadcastAnchorPageQuery.getAnchorStatus() != null, BroadcastAnchorDO::getAnchorStatus, broadcastAnchorPageQuery.getAnchorStatus())
                .like(StringUtils.isNotBlank(broadcastAnchorPageQuery.getAnchorName()), BroadcastAnchorDO::getAnchorName, broadcastAnchorPageQuery.getAnchorName())
                .ge(StringUtils.isNotBlank(broadcastAnchorPageQuery.getCreateTimeStart()), BroadcastAnchorDO::getCreateTime, broadcastAnchorPageQuery.getCreateTimeStart())
                .le(StringUtils.isNotBlank(broadcastAnchorPageQuery.getCreateTimeEnd()), BroadcastAnchorDO::getCreateTime, broadcastAnchorPageQuery.getCreateTimeEnd())
        );
        Page<BroadcastAnchorVO> broadcastAnchorVOPage = BeanUtil.convertPage(pages, BroadcastAnchorVO.class);
        List<BroadcastAnchorDO> doRecords = pages.getRecords();
        List<BroadcastAnchorVO> broadcastAnchorVOS = JsonUtils.jsonToList(doRecords, BroadcastAnchorVO.class);
        wrapperVO(broadcastAnchorVOS);
        broadcastAnchorVOPage.setRecords(broadcastAnchorVOS);
        return broadcastAnchorVOPage;
    }

    @Override
    public Boolean add(BroadcastAnchorParam broadcastAnchorParam) {
        wrapperParam(broadcastAnchorParam);
        checkUnique(broadcastAnchorParam);
        BroadcastAnchorDO broadcastAnchorDO = BeanUtil.convertBean(broadcastAnchorParam, BroadcastAnchorDO.class);
        broadcastAnchorDO.setCreateBy(UserUtils.getUserId());
        broadcastAnchorDO.setCreateTime(LocalDateTime.now());
        broadcastAnchorDO.setModifyTime(LocalDateTime.now());
        broadcastAnchorDO.setAnchorStatus(PublishStatusEnum.DISABLE.getValue());
        return broadcastAnchorMapper.insert(broadcastAnchorDO) > 0;
    }

    @Override
    public Boolean modify(BroadcastAnchorParam broadcastAnchorParam) {
        wrapperParam(broadcastAnchorParam);
        checkUnique(broadcastAnchorParam);
        BroadcastAnchorDO broadcastAnchorDO = BeanUtil.convertBean(broadcastAnchorParam, BroadcastAnchorDO.class);
        broadcastAnchorDO.setModifyBy(UserUtils.getUserId());
        broadcastAnchorDO.setModifyTime(LocalDateTime.now());
        return broadcastAnchorMapper.updateById(broadcastAnchorDO) > 0;
    }

    @Override
    public BroadcastAnchorVO view(Long id) {
        BroadcastAnchorDO broadcastAnchorDO = broadcastAnchorMapper.selectById(id);
        BroadcastAnchorVO broadcastAnchorVO = JsonUtils.fromJson(broadcastAnchorDO, BroadcastAnchorVO.class);
        wrapperVO(Collections3.newArrayList(broadcastAnchorVO));
        return broadcastAnchorVO;
    }

    @Override
    public Boolean changeStatus(Long id, Integer status) {
        BroadcastAnchorDO broadcastAnchorDO = new BroadcastAnchorDO();
        broadcastAnchorDO.setId(id);
        broadcastAnchorDO.setAnchorStatus(status);
        broadcastAnchorDO.setModifyTime(LocalDateTime.now());
        return broadcastAnchorMapper.updateById(broadcastAnchorDO) > 0;
    }

    @Override
    public Boolean del(Long id) {
        return broadcastAnchorMapper.deleteById(id) > 0;
    }

    public void wrapperParam(BroadcastAnchorParam param) {
    }

    public void wrapperVO(List<BroadcastAnchorVO> vos) {
        if (Collections3.isEmpty(vos)) {
            return;
        }
        //查询字典经营模式
        List<DictDTO> manageDictList = Optional.ofNullable(globalClient.queryDictByType(DictTypeEnum.BROADCAST_ANCHOR_ROLE.getValue())).map(ResponseResult::getData).orElse(Collections3.emptyList());
        Map<String, String> manageDictMap = manageDictList.parallelStream().collect(Collectors.toMap(DictDTO::getValue, DictDTO::getLabel, (s, a) -> a));

        for (BroadcastAnchorVO vo : vos) {
            Integer anchorRole = vo.getAnchorRole();
            vo.setAnchorSexName(SexEnum.getDescByCode(vo.getAnchorSex()));
            vo.setAnchorStatusDesc(PublishStatusEnum.getName(vo.getAnchorStatus()));
            if (anchorRole != null) {
                vo.setAnchorRoleName(manageDictMap.get(vo.getAnchorRole().toString()));
            }
        }
    }

    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(BroadcastAnchorParam param) {
        LambdaQueryWrapper<BroadcastAnchorDO> queryWrapper = new LambdaQueryWrapper<BroadcastAnchorDO>()
                .ne(param.getId() != null, BroadcastAnchorDO::getId, param.getId())
                .eq(StringUtils.isNotBlank(param.getAnchorName()), BroadcastAnchorDO::getAnchorName, param.getAnchorName());
        List<BroadcastAnchorDO> dos = broadcastAnchorMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在相同名字的主播!");
        }
    }
}
